$(function () {
    obj.init();
});

var obj = new function () {
    return {
        goodsForm: null,
        modify: null,
        id: null,
        init: function () {
            this.goodsForm = new xhForm($('#xh_form'));
            this.goodsForm.parse().bindRequired($('#bt_modal_confirm',parent.document));
            this.funObj().addUserData();
            this.listener();
        },
        listener: function () {
            var $this = this;
            $('#bt_modal_confirm',parent.document).click(function () {
                $this.funObj().saveData();
            });
            $('#goodsType').on('change',function () {//修改类型
                $this.funObj().getGoods();
            });
            $('#goodsName').on('change',function () {//修改物品
                $this.funObj().getUnit();
            });
        },
        funObj: function () {
            var form = this.goodsForm;
            var $this = this;
            return {
                addUserData: function () {
                    xhExtra.ajaxRequest('lm/goods/getUserData', {}, {check:true}, function (data) {
                        $('#buyUser').select2({placeholder: '选择人员', data: data, allowClear: true});
                    });
                    $('#goodsType').select2({placeholder: '请选择',width: 180});
                    $('#goodsName').select2({placeholder: '请选择',width: 180});
                },
                getGoods: function () {
                    var goodsType = $('#goodsType').val();
                    if (goodsType < 0){
                        window.top.xhTop.showError('物品id为空');
                        return false;
                    }
                    xhExtra.ajaxRequest('lm/goods/getGoodsByType', {goodsType: goodsType}, {check:false}, function (data) {
                        $this.funObj().fillSelect('goodsName',data);
                    });
                    $('#unit').html('');
                    $('#xh_form').find('#phoneNum').val('');
                    document.getElementById('phoneNumTr').style.display = 'none';
                },
                getUnit: function () {
                    var goodsId = $('#goodsName').val();
                    var goodsName = $('#goodsName').find("option:selected").text();
                    if (goodsId >= 1){
                        xhExtra.ajaxRequest('lm/goods/getUnitData', {goodsId: goodsId}, {check:false}, function (data) {
                            $('#unit').html(data);
                        });
                        if (goodsName === '手机卡') {
                            document.getElementById('phoneNumTr').style.display = 'contents';
                        } else {
                            $('#xh_form').find('#phoneNum').val('');
                            document.getElementById('phoneNumTr').style.display = 'none';
                        }
                    } else {
                        $('#unit').html('');
                    }
                },
                fillSelect: function (selector,data) {
                    var str = '<option value="" style="display: none" selected>请选择</option>';
                    if (data !== null){
                        $.each(data,function (key,val) {
                            str += '<option value="'+key+'">'+val+'</option>';
                        })
                    }
                    $('#'+selector).empty().append(str);
                },
                saveData: function () {
                    var goodsId = $('#goodsName').val();
                    var num = $('#num').val();
                    var buyUser = $('#buyUser').val();
                    var inType = 1;
                    if (goodsId < 1){
                        window.top.xhTop.showError('请选择商品');
                        xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                        return false;
                    }
                    if (num < 1){
                        window.top.xhTop.showError('请填写数量');
                        xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                        return false;
                    }
                    if (buyUser < 1){
                        window.top.xhTop.showError('请选择人员');
                        xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                    }
                    var goodsName = $('#goodsName').find("option:selected").text();
                    var phoneNum = $('#phoneNum').val();
                    if (goodsName === '手机卡' && !phoneNum) {
                        window.top.xhTop.showError('请填写手机号');
                        xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                    }
                    xhExtra.ajaxRequest('lm/goods/saveInGoodsData', {goodsId:goodsId, num:num, price:$('#price').val(),inType:inType,
                            size:$('#size').val(), status:$('#status').val(),buyUser:buyUser,time:$('#time').val(),remark:$('#remark').val(),
                            goodsName:goodsName,phoneNum:phoneNum},
                        {success: true,check:false},function (res) {
                            if(res.code === 200){
                                window.top.swal('成功',res.msg,'success');
                                window.top.xhTop.closeModal();
                                window.top.xhTop.getActiveFrame().obj.dataGrid.reload();
                            } else {
                                window.top.xhTop.showError(res.error);
                                xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                            }
                        });
                }
            }
        }
    }
};